package com.daocao.ds.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.util.DateTime;
import com.daocao.ds.util.TimeUtil;
import com.daocao.ds.util.Util;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class PayMoneyDilaog extends Dialog {


    @BindView(R.id.txt_time)
    SuperButton txtTime;
    @BindView(R.id.txt_balance)
    TextView txtBalance;
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.edit_pay_pwd)
    PayPwdEditText editPayPwd;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    private Context context;


    public PayMoneyDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public PayMoneyDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_pay_money);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.CENTER;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = (int) (Util.getDisplayMetrics(context).heightPixels * 0.85);
        } else {
            lp.width = (int) (Util.getDisplayMetrics(context).widthPixels * 0.85);
        }
        mWindow.setAttributes(lp);

        editPayPwd.initStyle(R.drawable.dialog_pass_bg, 6, 0.33f, R.color.dialog_password_bg, R.color.dialog_text_color, 32);
        editPayPwd.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {

            }
        });

        txtBalance.setText("可放款余额：" + UserInfoGlobal.getBalances() + "元");
    }

    public void setType(String type, String money) {
        if (type.equals("1")) {
            txtTips.setText("总共需放款");
            txtMoney.setText(money);
        } else {
            txtTips.setText("每日放款支付金额上限");
        }
    }

    public void setTime(String balance, String time) {
        txtBalance.setText("可放款余额：" + balance + "元");
        txtTime.setVisibility(View.VISIBLE);
        txtTime.setText("支付倒计时：" + TimeUtil.format(new Date(Long.valueOf(time) * 1000)));
    }

    private OnChooseListener onChooseListener;

    public PayMoneyDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    @OnClick({R.id.btn_close, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.btn_submit:
                if (editPayPwd.getPwdText().length() == 6) {
                    if (onChooseListener != null) {
                        onChooseListener.onSuccess(editPayPwd.getPwdText());
                    }
                }

                break;
        }
    }


    public interface OnChooseListener {
        void onSuccess(String pass);
    }
}
